<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :model="filters" :inline="true">
        <el-form-item>
          <el-input v-model="filters.keywords" placeholder="关键字" ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getTenants">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表v-loading="listLoading"-->
    <el-table :data="tenants" v-loading="listLoading" highlight-current-row @selection-change="handleSelectionChange" style="width: 100%;" >
      <!--多选框-->
      <el-table-column type="selection" width="55">
      </el-table-column>
      <!--索引值,为什么不用id,id不序号-->
      <el-table-column type="index" width="60">
      </el-table-column>
      <!--其他都设置值,只有一个不设置值就自动适应了-->
      <el-table-column prop="companyName" label="机构名称">
      </el-table-column>
      <el-table-column prop="companyNum" label="营业执照">
      </el-table-column>
      <el-table-column prop="address" label="公司地址">
      </el-table-column>
      <el-table-column prop="logo" label="LOGO">

      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small"  @click="edit(scope.$index,scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="del(scope.$index,scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" :disabled="this.sels.length===0" @click="delBatch">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"  :page-size="10" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增界面-->
    <el-dialog title="操作" :visible.sync="addFormVisible"  :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-form-item prop="companyName"label="公司名称">
          <el-input type="text" v-model="addForm.companyName" auto-complete="off" placeholder="请输入公司名称！"></el-input>
        </el-form-item>
        <el-form-item prop="companyNum" label="公司执照">
          <el-input type="text" v-model="addForm.companyNum" auto-complete="off" placeholder="请输入执照号码！"></el-input>
        </el-form-item>
        <el-form-item prop="address" label="公司地址">
          <el-input type="text" style="width: 350px" v-model="addForm.address" auto-complete="off" placeholder="请输入地址！"></el-input>
          <el-button size="small" type="primary" @click="selectAdrress">选择</el-button>
        </el-form-item>
        <el-form-item prop="logo" label="公司Logo">
          <!--<el-input type="text" v-model="employee.logo" auto-complete="off" placeholder="请输入logo！"></el-input>-->
          <el-upload
              class="upload-demo"
              action="http://localhost:1020/hrm/file/file/upload"
              name="fileName"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              :file-list="fileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
    <el-dialog
        title="选择地址"
        :visible.sync="dialogVisable"
        width="30%">
      <baidu-map :center="{lng: 116.403765, lat: 39.914850}" :zoom="11">
        <bm-view class="map"></bm-view>
        <bm-control :offset="{width: '10px', height: '10px'}">
          <!--:sugStyle="{zIndex: 2100} 让搜索提示上浮-->
          <bm-auto-complete v-model="filters.keyword" :sugStyle="{zIndex: 2100}">
            <div style="margin-bottom:10px">
              <input id="searchInput" type="text" placeholder="请输入关键字" class="searchinput"/>
              <el-button type="success" @click="selectAdrressConfirm">确定</el-button>
            </div>
          </bm-auto-complete>
        </bm-control>
        <bm-local-search :keyword="filters.keyword" :auto-viewport="true" ></bm-local-search>
      </baidu-map>
      <span slot="footer" class="dialog-footer">
						<el-button @click.native="dialogVisable=false">取 消</el-button>
						<el-button type="primary" @click.native="dialogVisable=false">确 定</el-button>
      				</span>
    </el-dialog>
  </section>
</template>

<script>
export default {
  data() {
    return {
      sels: [], //存储多选记录
      listLoading:false,
      dialogVisable:false,
      //查询对象
      filters:{
        keywords:''
      },
      page:1,//当前页,要传递到后台的
      total:0, //分页总数
      tenants:[], //当前页数据

      addFormVisible: false,//新增界面是否显示
      addLoading: false,
      addFormRules: {
        name: [
          { required: true, message: '请输入名字', trigger: 'blur' }
        ]
      },
      fileList: [],
      //新增界面数据
      addForm: {
        companyName: '',
        companyNum: '',
        address: '',
        logo:'/xx.jpg'
      }
    }
  },
  methods: {
    //表格多条数据选中事件
    handleSelectionChange(val) {
      /**
       * val中存放的是所有已选择的行数据集合
       */
      this.sels = [];
      if(val != null && val.length > 0){
        for(var i=0; i<val.length; i++){
          //往sels数组中存值
          this.sels.push(val[i].id);
        }
      }
    },
    selectAdrressConfirm(){
      //获取值搜索框值,设置给地址
      var searchInputV=document.getElementById("searchInput").value;
      this.addForm.address = searchInputV;
      //关闭对话框
      this.dialogVisable = false;
    },
    selectAdrress(){
      this.dialogVisable = true;
    },
    handleSuccess(response, file, fileList){
      if(response.success){
        this.addForm.logo = response.data.imagePath;
      }else{
        this.$message({
          message: '上传失败!',
          type: 'error'
        });
      }
    },
    handleRemove(file, fileList) {
      var path = file.response.data.realName;
      this.$http.post("/file/file/remove/" + path, null).then((res) => {
        if(res.data.success){
          this.$message({
            message: '操作成功!',
            type: 'success'
          });
        }
        else{
          this.$message({
            message: res.data.message,
            type: 'error'
          });
        }

      });

    },
    handlePreview(file) {
      console.log(file);
    },
    //删除
    del: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = { id: row.id };
        this.$http.delete('/system/tenant/'+row.id).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          //刷新列表
          this.getTenants();
        });
      }).catch(() => {

      });
    },
    //批量删除
    delBatch(){
      this.$confirm('确定删除这些数据吗？', '请确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let param = {
          "ids": this.sels
        };
        this.$http.post("/system/tenant/deleteBatch", param).then(result=>{
          if(result.data.success){
            this.$message({
              type: 'success',
              message: result.data.message
            });
            this.getTenants();
          }else{
            this.$message({
              type: 'error',
              message: result.data.message
            });
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
    //显示编辑界面
    edit(index, row) {
      //显示编辑页面的模态框
      this.addFormVisible = true;
      //Object.assign 是拷贝对象 BeanUtil.copyof()
      this.addForm = row;
      console.log("当前修改的数据是:"+JSON.stringify(this.addForm))
    },
    //显示新增界面
    handleAdd() {
      this.addFormVisible = true;
      this.addForm = {
        companyName: '',
        companyNum: '',
        address: '',
        logo:''
      };
    },

    //新增
    addSubmit: function () {
      //$refs调用js 原生dom元素
      //validate
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            //这里才是我们处理数据的地方
            this.addLoading = true;
            //Object.assign 复制对象的
            let para = Object.assign({}, this.addForm);
            this.$http.post("/system/tenant/save",para).then((res) => {
              this.addLoading = false;
              //elementUi的提示弹框
              this.$message({
                message: '提交成功',
                type: 'success'
              });
              //重新set数据
              //this.$refs['addForm'].resetFields();
              this.addFormVisible = false;
              this.getTenants();
            });
          });
        }
      });
    },
    handleCurrentChange(curentPage){
      this.page = curentPage;
      this.getTenants();
    },
    getTenants(){
      //发送Ajax请求后台获取数据  axios
      //添加分页条件及高级查询条件
      let para = {
        "page":this.page,
        "keyword":this.filters.keywords
      };
      this.listLoading = true; //显示加载圈
      //分页查询
      this.$http.post("/system/tenant/pagelist",para) //$.Post(.....)
          .then(result=>{
            this.total = result.data.data.total;
            this.tenants = result.data.data.rows;
            this.listLoading = false;  //关闭加载圈
          });
      /* 查询所有
      this.$http.patch("/department",para) //$.Post(.....)
        .then(result=>{
                      console.log(result);
                      console.log(result.data);
                      this.tenants = result.data;
                      this.listLoading = false;  //关闭加载圈
                  });
                  */
    }
  },
  mounted() {
    this.getTenants()
  }
}

</script>

<style lang="scss" scoped>
.login-container {
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 500px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  .title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
  .remember {
    margin: 0px 0px 35px 0px;
  }
}

.map {
  width: 100%;
  height: 300px;
}
.searchinput{
  width: 300px;
  box-sizing: border-box;
  padding: 9px;
  border: 1px solid #dddee1;
  line-height: 20px;
  font-size: 16px;
  height: 38px;
  color: #333;
  position: relative;
  border-radius: 4px;
}
</style>